package com.wokeMy.view.channe.huicao;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.woke.MainActivity;
import com.zhongcai.online.R;
import com.wokeMy.view.util.Util;

public class LotteryHtmlActivity extends Activity {

    private String url, urlname;
    private WebView mWebView = null;
    private Intent intent;
    private Handler handler;
    private Dialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lottery_html);
        dialog= Util.createLoadingDialog(LotteryHtmlActivity.this);
        intent = getIntent();
        intvkew();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断是否可以返回操作
        if (mWebView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            mWebView.goBack();

        } else {
            mWebView.destroy();
            finish();
        }
        return true;

    }

    @SuppressLint("JavascriptInterface")
    private void intvkew() {
        findViewById(R.id.back_lottery_iv).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(LotteryHtmlActivity.this,
//                        MainActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);
                finish();
            }
        });


        url = intent.getStringExtra("html");
//        url = "alipays://platformapi/startApp?appId=10000011&url=http%3A%2F%2Fjh.yizhibank.com %2Fapi%2FcreateOrder%3FmerchantOutOrderN%3D92Hc2018030931022%26merid%3Dyft2018022600004%26noncestr%3D12345678910%26notifyUrl%3Dhttp%3A%2F%2F120.78.79.235 %2Findex.php%2FApi%2FHuichao%2FcbBack%26orderMoney%3D1.30%26orderTime%3D20180309111405%26sign%3D900869a78875744932afa9083a6bd831";
        Log.e("urlname", "$" + url);
        mWebView = (WebView) findViewById(R.id.webView1_lottery_wv);
        WebSettings settings = mWebView.getSettings();
        setSettings(settings);
        mWebView.setWebViewClient(new MyWebViewClient());
        //		mWebView.loadUrl("http://www.88china.com");
        if (!TextUtils.isEmpty(url)) {
            mWebView.loadDataWithBaseURL(null, url, "text/html" , "utf-8", null);
//            if (url.startsWith("http")) {
//                mWebView.loadUrl(url);
//            } else {
//                mWebView.loadUrl("http://" + url);
//            }
        }
    }
    private void setSettings(WebSettings settings) {
        settings.setJavaScriptEnabled(true);// 设置可以运行JS脚本
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setSupportZoom(true);
        settings.setUseWideViewPort(true);
        settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        mWebView.setBackgroundColor(getResources().getColor(R.color.white)); // 设置背景色
        //		mWebView.getBackground().setAlpha(50); // 设置填充透明度 范围：0-255
        //		settings.setSupportZoom(false);// 用于设置webview放大
        //		settings.setBuiltInZoomControls(false);
        settings.setDomStorageEnabled(true);
        //		settings.setTextSize (TextSize.NORMAL);
        ////		settings.setPluginState(PluginState.ON);
        settings.setDatabaseEnabled(true);
    }


    // 监听
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null) {
                Log.e("UrlLoading",url);
                //                Uri uri = Uri.parse(url);
                //                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                //                startActivity(intent);
                view.loadUrl(url);
            }
            return false;

        }
        @Override
        public void onPageFinished(WebView view, String url) {
            view.getSettings().setJavaScriptEnabled(true);
            mWebView.setVisibility(View.VISIBLE);
            Util.closeDialog(dialog);
            Log.e("onPageFinishedurl......",url);
            Log.e("onPageFinishedgetUrl",view.getUrl());
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            view.getSettings().setJavaScriptEnabled(true);
            Log.e("onPageStarted",url);
            if(url.contains("/weixin.html")||view.getUrl().contains("/weixin.html")){
                        Intent intent = new Intent(LotteryHtmlActivity.this,
                                MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
            }
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(null, 1, "访问失败", null);
            Log.e("onReceivedError......",failingUrl);
        }
    }




}
